package com.thread;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.android.scj.tc.Connection;
import com.android.scj.tc.TcAppFolw;
import com.android.scj.tc.TerminalService;
import com.baseutils.File_Z;
import com.baseutils.Write_Message;
import com.bean.TradeMessage;
import com.util.DelayTimeThread;

public class FalsePurchaseThread2 extends Thread {

    private static TcAppFolw equiCon;
    // 设备通信业务类
    private static TerminalService terService = TerminalService.getService();
    // PSAM卡号
    private String pasm = "255"; //00
    // 通信类
    private static Connection con = Connection.getConnection();

    public DelayTimeThread delaytime = null;


    public static Bundle bundle;
    private static Handler handler; //00
    public Context context;
    Boolean aBoolean;

    public FalsePurchaseThread2(Handler handler, Context context, Boolean aBoolean, int nTotal, int sleepTime) {
        this.handler = handler;
        this.context = context;
        this.aBoolean = aBoolean;
        ;
        tradeMessage.setnTradeTotal(nTotal);

        equiCon = new TcAppFolw(context, aBoolean);
    }

    public void falsePurchase(int nFlagPsamInit) throws Exception {
        //time = System.currentTimeMillis();

        equiCon.ObuOfflineConsume(nFlagPsamInit,null);

        //long lTmpTime = System.currentTimeMillis();
        //time =  lTmpTime - time;

    }

    public void Psam(int nFlagPsamInit) throws Exception {
        //time = System.currentTimeMillis();

        equiCon.ObuOfflineConsume(nFlagPsamInit,null);

        //long lTmpTime = System.currentTimeMillis();
        //time =  lTmpTime - time;

    }

    private void LoadBundMess() throws Exception {

    }

    long timestart, timesend, time;

    public boolean isRun() {
        return isRun;
    }

    public void setRun(boolean run) {
        isRun = run;
    }

    boolean isRun = false;

    private int nTradeCount = 0;
    private int nTradeSuccessNumber = 0;
    private int nTradeFailNumber = 0;
    public TradeMessage tradeMessage = new TradeMessage();

    @Override
    public void run() {
        isRun = true;
        nTradeCount = 0;
        nTradeSuccessNumber = 0;
        nTradeFailNumber = 0;
        try {
            equiCon.PsamInit();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Message message = new Message();
        try {
            equiCon.setStrMacId("");
            falsePurchase(0);
            timesend = System.currentTimeMillis();
            time = timesend - TcAppFolw.timestart;
            LoadBundMess();
            tradeMessage.setMessage("零元消费成功");
            message.what = 1;
        } catch (Exception e) {
            timesend = System.currentTimeMillis();
            time = timesend - TcAppFolw.timestart;
            try {
                LoadBundMess();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Write_Message.write(File_Z.dirThreeFile.toString(), context, "零元消费失败：" + e.getMessage(), File_Z.time2);
            tradeMessage.setMessage("零元消费失败：" + e.getMessage());
            message.what = 2;
        }
        tradeMessage.setMac(equiCon.GetOBUMAC());
        tradeMessage.setTime("" + time);
        tradeMessage.setnTradeSuccessNumber(nTradeSuccessNumber);
        tradeMessage.setnTradeFailNumber(nTradeFailNumber);
        tradeMessage.setnTradeCount(nTradeCount);
        message.obj = tradeMessage;
        handler.sendMessage(message);

    }

}
